/*
 * Copyright (C) 2014 VenomVendor <info@VenomVendor.com>
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */

package com.naizhuai.ui.adapter.business;

import android.content.Context;
import android.os.Handler;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.naizhuai.R;
import com.naizhuai.ui.components.CircleImageView;
import com.naizhuai.ui.components.LabelsView;
import com.naizhuai.ui.components.SingleLabelsView;
import com.naizhuai.ui.entity.mainbusiness.TargetDetailsBean;
import com.naizhuai.util.ImageUtils;
import com.naizhuai.util.MathUtils;
import com.naizhuai.util.NaizhuAILog;

import java.util.ArrayList;
import java.util.List;

public class TargetDetailsAdapterHolder extends RecyclerView.Adapter {

    private List<TargetDetailsBean.TaskBeanForDetails> taskList;

    private Context mContext;
    private OnItemClickListener mItemClickListener;
    private static int TIME_DURATION = 20;
    private static int TIME_INTERVAL = 18;

    public TargetDetailsAdapterHolder(Context context, List<TargetDetailsBean.TaskBeanForDetails> taskList) {
        this.mContext = context;
        this.taskList = taskList;
    }


    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View sView = LayoutInflater.from(parent.getContext()).inflate(R.layout.adapter_target_details_task_item, parent, false);
        return new TargetMissionItemHolder(sView);
    }


    //targetBean
    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        TargetMissionItemHolder missionHolder = (TargetMissionItemHolder) holder;
        ImageUtils.glideThumbViewByUrl(mContext,missionHolder.chargeUserThumb,taskList.get(position).getManagerThumb());
        missionHolder.targetName.setText(taskList.get(position).getTaskTitle());
        missionHolder.joinedNumber.setText(getFormatStr(R.string.join_person_format, nullStr(taskList.get(position).getJoinedCount())));
        missionHolder.taskFinished.setText(getFormatStr(R.string.change_action_format, nullStr(taskList.get(position).getJoinedCount())));
        nullStr(MathUtils.numberSubstration(taskList.get(position).getActionTotalNumber(), taskList.get(position).getActionFinishedNumber()));
        double radiusUp = MathUtils.numberSubstrationReNUmber(System.currentTimeMillis()/1000 + "", taskList.get(position).getStartTime());
        double radiusDown = MathUtils.numberSubstrationReNUmber(taskList.get(position).getEndTime(), taskList.get(position).getStartTime());
        NaizhuAILog.e("EndTime="+taskList.get(position).getEndTime()+";StartTime="+taskList.get(position).getStartTime()+";currentTime="+System.currentTimeMillis());
        NaizhuAILog.e("radiusUp="+radiusUp+";radiusDown="+radiusDown);
        double timeRadius = radiusUp / radiusDown;
        NaizhuAILog.e("action_timeRadius="+timeRadius);
        missionHolder.itemView.post(new Runnable() {
            @Override
            public void run() {
                int totalWidth = missionHolder.processTotal.getWidth();
                int timeTotalWidth = (int) (totalWidth * timeRadius);
                int timeWidth =3;
//                NaizhuAILog.e("proRadius="+proRadius+".....timeRadius="+timeRadius);
//                NaizhuAILog.e("totalWidth="+totalWidth+"------proTotalWidth="+proTotalWidth+"======timeTotalWidth="+timeTotalWidth+";;;;;processWidth="+processWidth+"......timeWidth="+timeWidth);
                if (timeWidth < timeTotalWidth){
                    new Handler().post(new AddRunnable(timeTotalWidth, timeWidth, (timeTotalWidth - timeWidth) / TIME_DURATION, missionHolder.actionProcess));
                }
            }
        });
    }

    private String nullStr(String str) {
        return TextUtils.isEmpty(str) ? "0" : str;
    }

    private String getFormatStr(int resource, String... str) {
        String detailsStrFormat = mContext.getString(resource);
        return String.format(detailsStrFormat, (Object[]) str);
    }

    @Override
    public int getItemCount() {
        return taskList.size();
    }

    public class TargetMissionItemHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
        CircleImageView chargeUserThumb;
        TextView targetName, joinedNumber, taskFinished;
        View processTotal, actionProcess;

        public TargetMissionItemHolder(View view) {
            super(view);
            chargeUserThumb = (CircleImageView) view.findViewById(R.id.task_charge_thumb);
            targetName = (TextView) view.findViewById(R.id.target_name);
            joinedNumber = (TextView) view.findViewById(R.id.join_person);
            taskFinished = (TextView) view.findViewById(R.id.task_finished);
            processTotal = view.findViewById(R.id.total_process_line);
            actionProcess = view.findViewById(R.id.process_line);
            view.setOnClickListener(this);
        }


        @Override
        public void onClick(View v) {
            int index = getAdapterPosition();
            if (mItemClickListener != null) {
                mItemClickListener.onItemClick(v, index);
            }
        }
    }

    public interface OnItemClickListener {
        void onItemClick(View view, int position);
    }

    public void setOnItemClickListener(OnItemClickListener mItemClickListener) {
        this.mItemClickListener = mItemClickListener;
    }

    class AddRunnable implements Runnable {
        private int total;
        private int source;
        private int interAdd;
        private View mView;

        public AddRunnable(int total, int source, int interAdd, View mView) {
            this.total = total;
            this.source = source;
            this.interAdd = interAdd;
            this.mView = mView;
        }

        @Override
        public void run() {
//            NaizhuAILog.e("start.............");
            if (source < total) {
                source += interAdd;
                source = source > total ? total : source;
            } else
                return;
            if (source <= total) {
                RelativeLayout.LayoutParams params= (RelativeLayout.LayoutParams) mView.getLayoutParams();
                params.width=source;
                mView.setLayoutParams(params);
                new Handler().postDelayed(new AddRunnable(total, source, interAdd, mView), TIME_INTERVAL);
            }
        }
    }
}
